window.onload = function (e) {
    /**魔方格*/
    var div_blocks = document.getElementsByClassName("cube-bloc");

    var btn_control = document.getElementById("btn-control");
    var btn_play = document.getElementsByClassName("model-buttons")[0];

    var btnU = document.getElementById("U");
    var btnL = document.getElementById("L");
    var btnR = document.getElementById("R");
    var btnD = document.getElementById("D");
    var btnF = document.getElementById("F");
    var btnB = document.getElementById("B");
    var btnU_ = document.getElementById("U-");
    var btnL_ = document.getElementById("L-");
    var btnR_ = document.getElementById("R-");
    var btnD_ = document.getElementById("D-");
    var btnF_ = document.getElementById("F-");
    var btnB_ = document.getElementById("B-");
    var btnTR = document.getElementById("TR");
    var btnTL = document.getElementById("TL");
    var btnTD = document.getElementById("TD");
    var btnTU = document.getElementById("TU");
    var btnLR = document.getElementById("LR");
    var btnRL = document.getElementById("RL");

    var btn_init = document.getElementById("init");
    var btn_break = document.getElementById("break");

    /**方块*/

    /**面*/
    var fF = document.getElementsByClassName("f")[0];
    var bF = document.getElementsByClassName("b")[0];
    var rF = document.getElementsByClassName("r")[0];
    var lF = document.getElementsByClassName("l")[0];
    var uF = document.getElementsByClassName("u")[0];
    var dF = document.getElementsByClassName("d")[0];

    var Front = document.getElementsByClassName("cf")[0];
    var Right = document.getElementsByClassName("cr")[0];
    var Up = document.getElementsByClassName("cu")[0];

    /**中心面*/
    var xc = document.getElementsByClassName("x-center")[0];
    var yc = document.getElementsByClassName("y-center")[0];
    var zc = document.getElementsByClassName("z-center")[0];

    /**魔方块*/
    var content = document.getElementsByClassName("content")[0];
    /**魔方盒子*/
    var box = document.getElementsByClassName("box")[0];

    var tf = "transform";


    /**魔方*/
    var cube = new Cube();

    /**颜色对应值*/
    var blue = 1;
    var green = 2;
    var orange = 3;
    var red = 4;
    var yellow = 5;
    var white = 6;

    //定时器间隔
    var itv = 1;

    fF.style.transform = "translateZ(149px)";
    bF.style.transform = "translateZ(-149px) rotateY(180deg)";
    rF.style.transform = "translateX(149px) rotateY(90deg)";
    lF.style.transform = "translateX(-149px) rotateY(-90deg)";
    uF.style.transform = "translateY(-149px) rotateX(90deg)";
    dF.style.transform = "translateY(149px) rotateX(-90deg)";

    Front.style.transform = "translateZ(149px)";
    Right.style.transform = "translateX(149px) rotateY(90deg)";
    Up.style.transform = "translateY(-149px) rotateX(90deg)";

    box.style.transform = "rotateX(-30deg) rotateY(-34deg) rotateZ(-3deg)";

    var fx, _fx, fy, _fy, rx, _rx, ry, _ry, ux, _ux, uy, _uy;


    function bind(obj) {
        obj.onmousedown = function (e) {
            fx = Math.floor(e.offsetX / 150);
            fy = Math.floor(e.offsetY / 150);
        }
        obj.onmouseup = function (ev) {
            _fx = Math.floor(ev.offsetX / 150);
            _fy = Math.floor(ev.offsetY / 150);
            var point = [fx, fy, _fx, _fy];
            var flag = "f";
            switch (obj) {
                case Front:
                    flag = "f";
                    break;
                case Right:
                    flag = "r";
                    break;
                case Up:
                    flag = "u";
                    break;
            }
            setTimeout(function () {
                moveRotate(flag, point);
            }, 0);
        }
        obj.ontouchstart = function (e) {
            fx = Math.floor(e.offsetX / 150);
            fy = Math.floor(e.offsetY / 150);
        }
        obj.ontouchend = function (ev) {
            _fx = Math.floor(ev.offsetX / 150);
            _fy = Math.floor(ev.offsetY / 150);
            var point = [fx, fy, _fx, _fy];
            var flag = "f";
            switch (obj) {
                case Front:
                    flag = "f";
                    break;
                case Right:
                    flag = "r";
                    break;
                case Up:
                    flag = "u";
                    break;
            }
            setTimeout(function () {
                moveRotate(flag, point);
            }, 0);
        }
    }

    function moveRotate(flag, point) {
        var p = point.toString();
        switch (p) {
            case "0,0,0,1":
                switch (flag) {
                    case "f":
                        rotateL(true);
                        break;
                    case "r":
                        rotateF(true);
                        break;
                    case "u":
                        rotateL(true);
                        break;
                }
                break;
            case "0,1,0,0":
                switch (flag) {
                    case "f":
                        rotateL(false);
                        break;
                    case "r":
                        rotateF(false);
                        break;
                    case "u":
                        rotateL(false);
                        break;
                }
                break;
            case "0,0,1,0":
                switch (flag) {
                    case "f":
                        rotateU(false);
                        break;
                    case "r":
                        rotateU(false);
                        break;
                    case "u":
                        rotateB(false);
                        break;
                }
                break;
            case "1,0,0,0":
                switch (flag) {
                    case "f":
                        rotateU(true);
                        break;
                    case "r":
                        rotateU(true);
                        break;
                    case "u":
                        rotateB(true);
                        break;
                }

                break;
            case "0,1,1,1":
                switch (flag) {
                    case "f":
                        rotateD(true);
                        break;
                    case "r":
                        rotateD(true);
                        break;
                    case "u":
                        rotateF(true);
                        break;
                }
                break;
            case "1,1,0,1":
                switch (flag) {
                    case "f":
                        rotateD(false);
                        break;
                    case "r":
                        rotateD(false);
                        break;
                    case "u":
                        rotateF(false);
                        break;
                }
                break;
            case "1,0,1,1":
                switch (flag) {
                    case "f":
                        rotateR(false);
                        break;
                    case "r":
                        rotateB(false);
                        break;
                    case "u":
                        rotateR(false);
                        break;
                }

                break;
            case "1,1,1,0":
                switch (flag) {
                    case "f":
                        rotateR(true);
                        break;
                    case "r":
                        rotateB(true);
                        break;
                    case "u":
                        rotateR(true);
                        break;
                }
                break;
            default:
                break;
        }
    }

    /**绑定触摸函数*/
    bind(Front);
    bind(Right);
    bind(Up);


    var controlFlag = true;
    btn_control.onclick = function () {
        if (controlFlag) {
            btn_play.style.display = "block";
            btn_control.innerHTML = "禁用控制按钮";
        } else {
            btn_play.style.display = "none";
            btn_control.innerHTML = "启用控制按钮";
        }
        controlFlag = !controlFlag;
    };


    function paint(cube, blocks) {
        for (var i = 0; i < 6; i++) {
            for (var j = 0; j < 4; j++) {
                switch (cube.Blocks[i][j]) {
                    case blue:
                        blocks[i * 4 + j].style.backgroundColor = "blue";
                        break;
                    case green:
                        blocks[i * 4 + j].style.backgroundColor = "green";
                        break;
                    case orange:
                        blocks[i * 4 + j].style.backgroundColor = "orange";
                        break;
                    case red:
                        blocks[i * 4 + j].style.backgroundColor = "red";
                        break;
                    case yellow:
                        blocks[i * 4 + j].style.backgroundColor = "yellow";
                        break;
                    case white:
                        blocks[i * 4 + j].style.backgroundColor = "white";
                        break;
                }
            }
        }
    }

    /**绘制初始魔方*/
    paint(cube, div_blocks);

    /**按钮*/
    {
        btn_break.onclick = function () {
            var count = getRandom(18, 100);
            for (var i = 0; i < count; i++) {
                cube.RotateRandom();
            }
            paint(cube, div_blocks);
        };

        btn_init.onclick = function () {
            cube.Init();
            paint(cube, div_blocks);
        };

        btnU.onclick = function (ev) {
            rotateU(true);
        }
        btnL.onclick = function (ev) {
            rotateL(true);
        }
        btnR.onclick = function (ev) {
            rotateR(true);
        }
        btnD.onclick = function (ev) {
            rotateD(true);
        }
        btnF.onclick = function (ev) {
            rotateF(true);
        }
        btnB.onclick = function (ev) {
            rotateB(true);
        }


        btnU_.onclick = function (ev) {
            rotateU(false);
        }
        btnL_.onclick = function (ev) {
            rotateL(false);
        }
        btnR_.onclick = function (ev) {
            rotateR(false);
        }
        btnD_.onclick = function (ev) {
            rotateD(false);
        }
        btnF_.onclick = function (ev) {
            rotateF(false);
        }
        btnB_.onclick = function (ev) {
            rotateB(false);
        }

        /**
         * TR →
         * TL ←
         * TD ↙
         * TU ↗
         * RL ↖
         * LR ↘
         * */

        btnTR.onclick = function (ev) {
            RotateY(true);
        }
        btnTL.onclick = function (ev) {
            RotateY(false);
        }
        btnTD.onclick = function (ev) {
            RotateX(false);
        }
        btnTU.onclick = function (ev) {
            RotateX(true);
        }
        btnRL.onclick = function (ev) {
            RotateZ(false);
        }
        btnLR.onclick = function (ev) {
            RotateZ(true);
        }
    }

    var rx0 = "0px 150px -150px";
    var rx1 = "0px 0px -150px";


    var ry0 = "150px 0px -150px";
    var ry1 = "0px 0px -150px";

    var rb = "right bottom";
    var lb = "left bottom";
    var rt = "right top";
    var lt = "left top";

    /**层旋转动画*/
    function rotateF(flag) {
        var deg = 0;
        div_blocks[0].style.transformOrigin = rb;
        div_blocks[1].style.transformOrigin = lb;
        div_blocks[2].style.transformOrigin = rt;
        div_blocks[3].style.transformOrigin = lt;
        //左侧边缘
        div_blocks[9].style.transformOrigin = rx0;
        div_blocks[11].style.transformOrigin = rx1;
        //上层边缘
        div_blocks[18].style.transformOrigin = ry0;
        div_blocks[19].style.transformOrigin = ry1;
        //右侧边缘
        div_blocks[12].style.transformOrigin = rx0;
        div_blocks[14].style.transformOrigin = rx1;
        //下层边缘
        div_blocks[20].style.transformOrigin = ry0;
        div_blocks[21].style.transformOrigin = ry1;
        //中层面
        var timer = setInterval(function () {
            if (flag) deg++;
            else deg--;
            //上层旋转
            div_blocks[18].style.transform = "rotateY(" + deg + "deg)";
            div_blocks[19].style.transform = "rotateY(" + deg + "deg)";
            //左侧旋转
            div_blocks[9].style.transform = "rotateX(" + deg + "deg)";
            div_blocks[11].style.transform = "rotateX(" + deg + "deg)";
            //右侧旋转
            div_blocks[12].style.transform = "rotateX(" + (-deg) + "deg)";
            div_blocks[14].style.transform = "rotateX(" + (-deg) + "deg)";
            //下层旋转
            div_blocks[20].style.transform = "rotateY(" + (-deg) + "deg)";
            div_blocks[21].style.transform = "rotateY(" + (-deg) + "deg)";

            for (var i = 0; i < 4; i++) {
                div_blocks[i].style.transform = "rotateZ(" + deg + "deg)";
            }
            if (deg === (flag ? 90 : -90)) {
                clearInterval(timer);
                if (flag) cube.RotF();
                else cube._RotF();
                cube.reset();
                paint(cube, div_blocks);
                for (var i = 0; i < 4; i++) {
                    div_blocks[i].style.transform = "";
                }
                //上层恢复
                div_blocks[18].style.transform = "";
                div_blocks[19].style.transform = "";
                //左侧恢复
                div_blocks[9].style.transform = "";
                div_blocks[11].style.transform = "";
                //右侧恢复
                div_blocks[12].style.transform = "";
                div_blocks[14].style.transform = "";
                //下层恢复
                div_blocks[20].style.transform = "";
                div_blocks[21].style.transform = "";
                //中层恢复
                xc[0].transform = "";
            }
        }, itv);
    }

    function rotateB(flag) {
        var deg = 0;
        div_blocks[4].style.transformOrigin = rb;
        div_blocks[5].style.transformOrigin = lb;
        div_blocks[6].style.transformOrigin = rt;
        div_blocks[7].style.transformOrigin = lt;
        //上层边缘
        div_blocks[16].style.transformOrigin = "150px 0px -150px";
        div_blocks[17].style.transformOrigin = "0px 0px -150px";
        //下层边缘
        div_blocks[22].style.transformOrigin = "150px 0px -150px";
        div_blocks[23].style.transformOrigin = "0px 0px -150px";
        //右侧边缘
        div_blocks[13].style.transformOrigin = "0px 150px -150px";
        div_blocks[15].style.transformOrigin = "0px 0px -150px";
        //左侧边缘
        div_blocks[8].style.transformOrigin = "0px 150px -150px";
        div_blocks[10].style.transformOrigin = "0px 0px -150px";
        var timer = setInterval(function () {
            if (flag) deg++;
            else deg--;
            //上层旋转
            div_blocks[16].style.transform = "rotateY(" + (-deg) + "deg)";
            div_blocks[17].style.transform = "rotateY(" + (-deg) + "deg)";
            //下层旋转
            div_blocks[22].style.transform = "rotateY(" + (deg) + "deg)";
            div_blocks[23].style.transform = "rotateY(" + (deg) + "deg)";
            //右侧旋转
            div_blocks[13].style.transform = "rotateX(" + (deg) + "deg)";
            div_blocks[15].style.transform = "rotateX(" + (deg) + "deg)";
            //左侧旋转
            div_blocks[8].style.transform = "rotateX(" + (-deg) + "deg)";
            div_blocks[10].style.transform = "rotateX(" + (-deg) + "deg)";
            for (var i = 4; i < 8; i++) {
                div_blocks[i].style.transform = "rotateZ(" + deg + "deg)";
            }
            if (deg === (flag ? 90 : -90)) {
                clearInterval(timer);
                if (flag) cube.RotB();
                else cube._RotB();
                cube.reset();
                paint(cube, div_blocks);
                for (var i = 4; i < 8; i++) {
                    div_blocks[i].style.transform = "";
                }
                //上层归位
                div_blocks[16].style.transform = "";
                div_blocks[17].style.transform = "";
                //下层归位
                div_blocks[22].style.transform = "";
                div_blocks[23].style.transform = "";
                //右侧归位
                div_blocks[13].style.transform = "";
                div_blocks[15].style.transform = "";
                //左侧归位
                div_blocks[8].style.transform = "";
                div_blocks[10].style.transform = "";
            }
        }, itv);
    }

    function rotateL(flag) {
        var deg = 0;
        div_blocks[8].style.transformOrigin = rb;
        div_blocks[9].style.transformOrigin = lb;
        div_blocks[10].style.transformOrigin = rt;
        div_blocks[11].style.transformOrigin = lt;

        //上层边缘
        div_blocks[16].style.transformOrigin = "0px 150px -150px";
        div_blocks[18].style.transformOrigin = "0px 0px -150px";
        //正面边缘
        div_blocks[0].style.transformOrigin = "0px 150px -150px";
        div_blocks[2].style.transformOrigin = "0px 0px -150px";
        //下层边缘
        div_blocks[20].style.transformOrigin = "0px 150px -150px";
        div_blocks[22].style.transformOrigin = "0px 0px -150px";
        //背面边缘
        div_blocks[5].style.transformOrigin = "0px 150px -150px";
        div_blocks[7].style.transformOrigin = "0px 0px -150px";
        var timer = setInterval(function () {
            if (flag) deg++;
            else deg--;
            //上层边缘
            div_blocks[16].style.transform = "rotateX(" + (-deg) + "deg)";
            div_blocks[18].style.transform = "rotateX(" + (-deg) + "deg)";
            //正面边缘
            div_blocks[0].style.transform = "rotateX(" + (-deg) + "deg)";
            div_blocks[2].style.transform = "rotateX(" + (-deg) + "deg)";
            //下层边缘
            div_blocks[20].style.transform = "rotateX(" + (-deg) + "deg)";
            div_blocks[22].style.transform = "rotateX(" + (-deg) + "deg)";
            //背面边缘
            div_blocks[5].style.transform = "rotateX(" + (deg) + "deg)";
            div_blocks[7].style.transform = "rotateX(" + (deg) + "deg)";
            for (var i = 8; i < 12; i++) {
                div_blocks[i].style.transform = "rotateZ(" + deg + "deg)";
            }
            if (deg === (flag ? 90 : -90)) {
                clearInterval(timer);
                if (flag) cube.RotL();
                else cube._RotL();
                cube.reset();
                paint(cube, div_blocks);
                for (var i = 8; i < 12; i++) {
                    div_blocks[i].style.transform = "";
                }
                //上层边缘
                div_blocks[16].style.transform = "";
                div_blocks[18].style.transform = "";
                //正面边缘
                div_blocks[0].style.transform = "";
                div_blocks[2].style.transform = "";
                //下层边缘
                div_blocks[20].style.transform = "";
                div_blocks[22].style.transform = "";
                //背面边缘
                div_blocks[5].style.transform = "";
                div_blocks[7].style.transform = "";
            }
        }, itv);
    }

    function rotateR(flag) {
        var deg = 0;
        div_blocks[12].style.transformOrigin = rb;
        div_blocks[13].style.transformOrigin = lb;
        div_blocks[14].style.transformOrigin = rt;
        div_blocks[15].style.transformOrigin = lt;
        //上层
        div_blocks[17].style.transformOrigin = rx0;
        div_blocks[19].style.transformOrigin = rx1;
        //下层
        div_blocks[21].style.transformOrigin = rx0;
        div_blocks[23].style.transformOrigin = rx1;
        //正面
        div_blocks[1].style.transformOrigin = rx0;
        div_blocks[3].style.transformOrigin = rx1;
        //背面
        div_blocks[4].style.transformOrigin = rx0;
        div_blocks[6].style.transformOrigin = rx1;
        var timer = setInterval(function () {
            if (flag) deg++;
            else deg--;
            //上层
            div_blocks[17].style.transform = "rotateX(" + deg + "deg)";
            div_blocks[19].style.transform = "rotateX(" + deg + "deg)";
            //下层
            div_blocks[21].style.transform = "rotateX(" + deg + "deg)";
            div_blocks[23].style.transform = "rotateX(" + deg + "deg)";
            //正面
            div_blocks[1].style.transform = "rotateX(" + deg + "deg)";
            div_blocks[3].style.transform = "rotateX(" + deg + "deg)";
            //背面
            div_blocks[4].style.transform = "rotateX(" + (-deg) + "deg)";
            div_blocks[6].style.transform = "rotateX(" + (-deg) + "deg)";
            for (var i = 12; i < 16; i++) {
                div_blocks[i].style.transform = "rotateZ(" + deg + "deg)";
            }
            if (deg === (flag ? 90 : -90)) {
                clearInterval(timer);
                if (flag) cube.RotR();
                else cube._RotR();
                cube.reset();
                paint(cube, div_blocks);
                for (var i = 12; i < 16; i++) {
                    div_blocks[i].style.transform = "";
                }
                //上层
                div_blocks[17].style.transform = "";
                div_blocks[19].style.transform = "";
                //下层
                div_blocks[21].style.transform = "";
                div_blocks[23].style.transform = "";
                //正面
                div_blocks[1].style.transform = "";
                div_blocks[3].style.transform = "";
                //背面
                div_blocks[4].style.transform = "";
                div_blocks[6].style.transform = "";
            }
        }, itv);
    }

    function rotateU(flag) {
        var deg = 0;
        div_blocks[16].style.transformOrigin = rb;
        div_blocks[17].style.transformOrigin = lb;
        div_blocks[18].style.transformOrigin = rt;
        div_blocks[19].style.transformOrigin = lt;
        //正面
        div_blocks[0].style.transformOrigin = ry0;
        div_blocks[1].style.transformOrigin = ry1;
        //背面
        div_blocks[4].style.transformOrigin = ry0;
        div_blocks[5].style.transformOrigin = ry1;
        //左侧
        div_blocks[8].style.transformOrigin = ry0;
        div_blocks[9].style.transformOrigin = ry1;
        //右侧
        div_blocks[12].style.transformOrigin = ry0;
        div_blocks[13].style.transformOrigin = ry1;
        var timer = setInterval(function () {
            if (flag) deg++;
            else deg--;
            //正面
            div_blocks[0].style.transform = "rotateY(" + (-deg) + "deg)";
            div_blocks[1].style.transform = "rotateY(" + (-deg) + "deg)";
            //背面
            div_blocks[4].style.transform = "rotateY(" + (-deg) + "deg)";
            div_blocks[5].style.transform = "rotateY(" + (-deg) + "deg)";
            //左侧
            div_blocks[8].style.transform = "rotateY(" + (-deg) + "deg)";
            div_blocks[9].style.transform = "rotateY(" + (-deg) + "deg)";
            //右侧
            div_blocks[12].style.transform = "rotateY(" + (-deg) + "deg)";
            div_blocks[13].style.transform = "rotateY(" + (-deg) + "deg)";

            for (var i = 16; i < 20; i++) {
                div_blocks[i].style.transform = "rotateZ(" + deg + "deg)";
            }
            if (deg === (flag ? 90 : -90)) {
                clearInterval(timer);
                flag ? cube.RotU() : cube._RotU();
                cube.reset();
                paint(cube, div_blocks);
                for (var i = 16; i < 20; i++) {
                    div_blocks[i].style.transform = "";
                }
                //正面
                div_blocks[0].style.transform = "";
                div_blocks[1].style.transform = "";
                //背面
                div_blocks[4].style.transform = "";
                div_blocks[5].style.transform = "";
                //左侧
                div_blocks[8].style.transform = "";
                div_blocks[9].style.transform = "";
                //右侧
                div_blocks[12].style.transform = "";
                div_blocks[13].style.transform = "";
            }
        }, itv);
    }

    function rotateD(flag) {
        var deg = 0;
        div_blocks[20].style.transformOrigin = rb;
        div_blocks[21].style.transformOrigin = lb;
        div_blocks[22].style.transformOrigin = rt;
        div_blocks[23].style.transformOrigin = lt;
        //正面
        div_blocks[2].style.transformOrigin = ry0;
        div_blocks[3].style.transformOrigin = ry1;
        //背面
        div_blocks[6].style.transformOrigin = ry0;
        div_blocks[7].style.transformOrigin = ry1;
        //左侧
        div_blocks[10].style.transformOrigin = ry0;
        div_blocks[11].style.transformOrigin = ry1;
        //右侧
        div_blocks[14].style.transformOrigin = ry0;
        div_blocks[15].style.transformOrigin = ry1;
        var timer = setInterval(function () {
            if (flag) deg++;
            else deg--;
            //正面
            div_blocks[2].style.transform = "rotateY(" + deg + "deg)";
            div_blocks[3].style.transform = "rotateY(" + deg + "deg)";
            //背面
            div_blocks[6].style.transform = "rotateY(" + deg + "deg)";
            div_blocks[7].style.transform = "rotateY(" + deg + "deg)";
            //左侧
            div_blocks[10].style.transform = "rotateY(" + deg + "deg)";
            div_blocks[11].style.transform = "rotateY(" + deg + "deg)";
            //右侧
            div_blocks[14].style.transform = "rotateY(" + deg + "deg)";
            div_blocks[15].style.transform = "rotateY(" + deg + "deg)";
            for (var i = 20; i < 24; i++) {
                div_blocks[i].style.transform = "rotateZ(" + deg + "deg)";
            }
            if (deg === (flag ? 90 : -90)) {
                clearInterval(timer);
                flag ? cube.RotD() : cube._RotD();
                cube.reset();
                paint(cube, div_blocks);
                for (var i = 20; i < 24; i++) {
                    div_blocks[i].style.transform = "";
                }
                //正面
                div_blocks[2].style.transform = "";
                div_blocks[3].style.transform = "";
                //背面
                div_blocks[6].style.transform = "";
                div_blocks[7].style.transform = "";
                //左侧
                div_blocks[10].style.transform = "";
                div_blocks[11].style.transform = "";
                //右侧
                div_blocks[14].style.transform = "";
                div_blocks[15].style.transform = "";
            }
        }, itv);
    }

    /**体旋转动画*/
    /**
     * TR →
     * TL ←
     * TD ↙
     * TU ↗
     * RL ↖
     * LR ↘
     * */


    function RotateX(flag) {
        var deg = 0;
        var timer = setInterval(function () {
            if (flag) deg++;
            else deg--;
            content.style.transform = "rotateX(" + deg + "deg)";
            if (deg === (flag ? 90 : -90)) {
                clearInterval(timer);
                content.style.transform = "";
                flag ? cube.TU() : cube.TD();
                cube.reset();
                paint(cube, div_blocks);
            }
        }, itv);
    }

    function RotateY(flag) {
        var deg = 0;
        var timer = setInterval(function () {
            if (flag) deg++;
            else deg--;
            content.style.transform = "rotateY(" + deg + "deg)";
            if (deg === (flag ? 90 : -90)) {
                clearInterval(timer);
                content.style.transform = "";
                flag ? cube.TR() : cube.TL();
                cube.reset();
                paint(cube, div_blocks);
            }
        }, itv);
    }

    function RotateZ(flag) {
        var deg = 0;
        var timer = setInterval(function () {
            if (flag) deg++;
            else deg--;
            content.style.transform = "rotateZ(" + deg + "deg)";
            if (deg === (flag ? 90 : -90)) {
                clearInterval(timer);
                content.style.transform = "";
                flag ? cube.LR() : cube.RL();
                cube.reset();
                paint(cube, div_blocks);
            }
        }, itv);
    }
};

//产生begin到end（不包含end）之间的随机数
function getRandom(begin, end) {
    var rd = Math.random() * (end - begin);
    rd = Math.floor(rd);
    rd += begin;
    return rd;
}

//魔方数据处理
function Cube() {
    var blue = 1;
    var green = 2;
    var orange = 3;
    var red = 4;
    var yellow = 5;
    var white = 6;


    var U = [yellow, yellow, yellow, yellow];
    var D = [white, white, white, white];
    var F = [blue, blue, blue, blue];
    var B = [green, green, green, green];
    var L = [orange, orange, orange, orange];
    var R = [red, red, red, red];


    this.Blocks = [F, B, L, R, U, D];

    this.RotateRandom = function () {
        var index = getRandom(1, 19);
        switch (index) {
            case 1:
                this.RotF();
                break;
            case 2:
                this.RotB();
                break;
            case 3:
                this.RotL();
                break;
            case 4:
                this.RotR();
                break;
            case 5:
                this.RotU();
                break;
            case 6:
                this.RotD();
                break;
            case 7:
                this._RotF();
                break;
            case 8:
                this._RotB();
                break;
            case 9:
                this._RotL();
                break;
            case 10:
                this._RotR();
                break;
            case 11:
                this._RotU();
                break;
            case 12:
                this._RotD();
                break;
            // case 13:
            //     this.TR();
            //     break;
            // case 14:
            //     this.TL();
            //     break;
            // case 15:
            //     this.TU();
            //     break;
            // case 16:
            //     this.TD();
            //     break;
            // case 17:
            //     this.RL();
            //     break;
            // case 18:
            //     this.LR();
            //     break;
        }
    }

    this.Init = function () {
        U = [yellow, yellow, yellow, yellow];
        D = [white, white, white, white];
        F = [blue, blue, blue, blue];
        B = [green, green, green, green];
        L = [orange, orange, orange, orange];
        R = [red, red, red, red];
        this.Blocks = [F, B, L, R, U, D];
    }

    this.reset = function () {
        this.Blocks = [F, B, L, R, U, D];
    }

    this.RotF = function () {
        var T = [F[0], F[1], F[2], F[3]];
        F[1] = T[0];
        F[3] = T[1];
        F[2] = T[3];
        F[0] = T[2];

        var P = [U[2], U[3]];
        U[2] = L[3];
        U[3] = L[1];
        L[1] = D[0];
        L[3] = D[1];
        D[0] = R[2];
        D[1] = R[0];
        R[0] = P[0];
        R[2] = P[1];
    }
    this.RotB = function () {
        var T = [B[0], B[1], B[2], B[3]];
        B[1] = T[0];
        B[3] = T[1];
        B[2] = T[3];
        B[0] = T[2];

        var P = [U[0], U[1]];
        U[0] = R[1];
        U[1] = R[3];
        R[1] = D[3];
        R[3] = D[2];
        D[2] = L[0];
        D[3] = L[2];
        L[0] = P[1];
        L[2] = P[0];
    }
    this.RotL = function () {
        var T = [L[0], L[1], L[2], L[3]];
        L[1] = T[0];
        L[3] = T[1];
        L[2] = T[3];
        L[0] = T[2];

        var P = [U[0], U[2]];
        U[0] = B[3];
        U[2] = B[1];
        B[3] = D[0];
        B[1] = D[2];
        D[0] = F[0];
        D[2] = F[2];
        F[0] = P[0];
        F[2] = P[1];
    }
    this.RotR = function () {
        var T = [R[0], R[1], R[2], R[3]];
        R[1] = T[0];
        R[3] = T[1];
        R[2] = T[3];
        R[0] = T[2];

        var P = [U[1], U[3]];
        U[1] = F[1];
        U[3] = F[3];
        F[1] = D[1];
        F[3] = D[3];
        D[1] = B[2];
        D[3] = B[0];
        B[2] = P[0];
        B[0] = P[1];
    }
    this.RotU = function () {
        var T = [U[0], U[1], U[2], U[3]];
        U[1] = T[0];
        U[3] = T[1];
        U[2] = T[3];
        U[0] = T[2];

        var P = [F[0], F[1]];
        F[0] = R[0];
        F[1] = R[1];
        R[0] = B[0];
        R[1] = B[1];
        B[0] = L[0];
        B[1] = L[1];
        L[0] = P[0];
        L[1] = P[1];
    }
    this.RotD = function () {
        var T = [D[0], D[1], D[2], D[3]];
        D[1] = T[0];
        D[3] = T[1];
        D[2] = T[3];
        D[0] = T[2];

        var P = [F[2], F[3]];
        F[2] = L[2];
        F[3] = L[3];
        L[2] = B[2];
        L[3] = B[3];
        B[2] = R[2];
        B[3] = R[3];
        R[2] = P[0];
        R[3] = P[1]
    }
    this._RotF = function () {
        this.RotF();
        this.RotF();
        this.RotF();
    }
    this._RotB = function () {
        this.RotB();
        this.RotB();
        this.RotB();
    }
    this._RotL = function () {
        this.RotL();
        this.RotL();
        this.RotL();
    }
    this._RotR = function () {
        this.RotR();
        this.RotR();
        this.RotR();
    }
    this._RotU = function () {
        this.RotU();
        this.RotU();
        this.RotU();
    }
    this._RotD = function () {
        this.RotD();
        this.RotD();
        this.RotD();
    }

    /**以上层为参照*/
    //横向
    this.TR = function () {
        this._RotU();
        this.RotD();
    }
    this.TL = function () {
        this.RotU();
        this._RotD();
    }
    //纵向
    this.TD = function () {
        this._RotR();
        this.RotL();
    }
    this.TU = function () {
        this.RotR();
        this._RotL();
    }
    //纵向侧旋转
    this.RL = function () {
        this._RotF();
        this.RotB();
    }
    this.LR = function () {
        this.RotF();
        this._RotB();
    }
}

